import Mock from 'mockjs' // 导入 mockjs
import {useSysTesPaperStore} from '../stores/testPaper.js'
import { storeToRefs } from 'pinia';
const useSysTesPaper = useSysTesPaperStore()

// 创建新的试题
Mock.mock("/api/tesPaper/add", 'post', (options) => {
    const obj = JSON.parse(options.body);
    useSysTesPaper.addPaper( obj);
    console.log(obj)
    return {
        code: 200,
        msg: '添加成功',
        data: obj,
    };
})

// 获取所有试题列表
Mock.mock("/api/tesPaper/all", 'get', (options) => {
    try {
        // const { getAllPapers } = storeToRefs(useSysTesPaper);
        const allPapers = useSysTesPaper.sysTesPaper
        return {
            code: 200,
            msg: '查询成功',
            data: allPapers,
        };
    }catch (e) {
        return {
            code: 500,
            msg: '查询失败',
            data: e,
        };
    }
})

// 根据查询条件查询
Mock.mock("/api/tesPaper/query", 'post', (options) => {
    const obj = JSON.parse(options.body);
    console.log( obj)
    try {
        const { findPapersByConditions  } = storeToRefs(useSysTesPaper);
        // 调用 getter，它会返回一个函数
        const findFunction = findPapersByConditions .value;
        // 执行这个函数并传入查询条件
        const filteredPapers = findFunction(obj);
        console.log('Mock 获取的查询结果:', filteredPapers)
        return {
            code: 200,
            msg: '查询成功',
            data: filteredPapers,
        };
    }catch (e) {
        return {
            code: 500,
            msg: '查询失败',
            data: e,
        };
    }
})

// 根据ID删除对应数据,多个
Mock.mock("/api/tesPaper/delete", 'post', (options) => {
    const ids = JSON.parse(options.body);
    for (let i = 0; i < ids.length; i++){
        useSysTesPaper.deletePaper(ids[i]);
    }
    console.log(
        'Mock 删除的试题ID:', ids
    )
})

// 更新试题
Mock.mock("/api/tesPaper/update", 'post', (options) => {
    const obj = JSON.parse(options.body);
    useSysTesPaper.updatePaper(obj);
    console.log(obj)
    return {
        code: 200,
        msg: '更新成功',
        data: null,
    }
})

